import Mock from 'mockjs'

// 处理url地址模块 ?a=100&b=200
const qs = require('qs')

// mock的配置
Mock.setup({
    // 模拟网络请求耗时：随机延时500-1000毫秒
    timeout: '500-1000'
})

// mock我的足迹
Mock.mock(/\/member\/browseHistory/, 'get', (config) => {
    const position = config.url.lastIndexOf('?')
    const search = config.url.substr(position + 1)
    const queryStr = qs.parse(search)
    const page = queryStr.page - 0 || 1
    const pageSize = queryStr.pageSize - 0 || 4

    // 模拟数据
    const {
        list
    } = Mock.mock({
        'list|100': [{
            id: '@increment',
            name: '@ctitle(10,20)',
            desc: '@ctitle(4,10)',
            picture: "@image('50*50','#FF83FA','#FCFCFC','png','mono')",
            price: '@float(100,200,2,2)'
        }]
    })

    // 计算总页数和总记录
    const counts = list.length
    const pages = Math.ceil(counts / pageSize)

    // 获取分页数据
    const start = (page - 1) * pageSize
    const end = pageSize * page
    const items = page > pages ? [] : list.slice(start, end)

    return {
        msg: '操作成功',
        result: {
            counts,
            pageSize,
            page,
            pages,
            items
        }
    }
})

// mock 我的收藏

Mock.mock(/\/member\/collect/, 'get', (config) => {
    // 接收筛选条件
    const position = config.url.lastIndexOf('?')
    const search = config.url.substr(position + 1)
    const queryStr = qs.parse(search)

    const page = queryStr.page - 0 || 1
    const pageSize = queryStr.pageSize - 0 || 4
    const collectType = queryStr.collectType - 0 || 1

    // 模拟数据
    let list = []
    if (collectType === 1) {
        list = Mock.mock({
            'list|75': [{
                id: '@increment',
                name: '@ctitle(10,20)',
                desc: '@ctitle(4,10)',
                picture: "@image('50*50','#FF83FA','#FCFCFC','png','mono')",
                price: '@float(100,200,2,2)'
            }]
        }).list
    }

    // 计算总页数和总记录
    const counts = list.length
    const pages = Math.ceil(counts / pageSize)

    // 获取分页数据
    const start = (page - 1) * pageSize
    const end = pageSize * page
    const items = page > pages ? [] : list.slice(start, end)

    return {
        msg: '操作成功',
        result: {
            counts,
            pageSize,
            page,
            pages,
            items
        }
    }
})